{% extends "layout.html" %}
{% block content %}
<div class="container-fluid">
    <div class="banner row">
        <div class="col">
            <br /><br />
            <h1>{% if config.FRESHERMEAT_INSTANCE_NAME %}{{ config.FRESHERMEAT_INSTANCE_NAME }}{% else %}Freshermeat{% endif %}</h1>
            <div class="float-right text-right">
                <h6>{{ nb_projects }} projects</h6>
                <h6>{{ nb_releases }} releases</h6>
                <h6>{{ nb_cves }} CVEs</h6>
            </div>
        </div>
    </div>
</div>
<div class="container">
    <div class="row">
        <div class="col">
            <br />
            <h3>Projects
                {% if current_user.is_authenticated %}
                <a href="{{ url_for('project_bp.form') }}">
                    <i class="fa fa-plus-circle" aria-hidden="true" style="color:black;" title="Create a project"></i>
                </a>
                {% else %}
                <a href="{{ url_for('submission_bp.form_submission') }}">
                    <i class="fa fa-plus-circle" aria-hidden="true" style="color:black;" title="Submit a project"></i>
                </a>
                {% endif %}
            </h3>
            <div id="list-projects" class="list-group"></div>
        </div>
        <div class="col">
            <br />
            <h3>Organizations
                {% if current_user.is_authenticated and current_user.is_admin %}
                <a href="{{ url_for('organization_bp.form') }}">
                    <i class="fa fa-plus-circle" aria-hidden="true" style="color:black;" title="Create an organization"></i>
                </a>
                {% endif %}
            </h3>
            <div id="list-organizations"></div>
            <h3>People</h3>
            <div id="list-people"></div>
        </div>
    </div>
</div><!-- /.container -->
<script>
$(document).ready(function() {
    $.ajax({ // Get the list of projects
        url: "{{ url_for('projectapi0.projectapi',) }}",
        dataType: "json",
        contentType: "application/json",
        success: function(projects) {
            projects.objects
            .sort(function (a, b) {
                return a.last_updated < b.last_updated;
            })
            .map(function (project) {
                var cardHTML = projectHomeTemplate({
                    'project_url': '/project/'+project.name,
                    'project_name': project.name,
                    'project_description': project.short_description,
                    'project_last_update': moment(project.last_updated).fromNow()
                 });
                 $('#list-projects').append(cardHTML);
            });
        }
    });

    $.ajax({ // Get the list of organizations
        url: "{{ url_for('organizationapi0.organizationapi',) }}",
        dataType: "json",
        contentType: "application/json",
        success: function(organizations) {
            organizations.objects
            .sort(function (a, b) {
                return a.last_updated < b.last_updated;
            })
            .map(function (organization) {
                var cardHTML = organizationTemplate({
                    'organization_url': '/organization/'+organization.name,
                    'organization_name': organization.name,
                    'organization_short_description': organization.short_description,
                    'organization_last_update': moment(organization.last_updated).fromNow()
                 });
                 $('#list-organizations').append(cardHTML);
            });
        }
    });

    $.ajax({ // Get the list of users
        url: "{{ url_for('userapi0.userapi',) }}",
        dataType: "json",
        contentType: "application/json",
        success: function(users) {
            users.objects
            .map(function (user) {
                var cardHTML = userTemplate({
                    'user_url': '/user/'+user.login,
                    'user_login': user.login
                 });
                 $('#list-people').append(cardHTML);
            });
        }
    });
});
</script>
{% endblock %}
